﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;
    using WMS6.Web.UserControls;

    public class BolDetailList : BasePage
    {
        protected Button btnList;
        protected Button btnSearch;
        protected HtmlForm form1;
        protected GridView gvBolDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2BolDetailList(BolDetailQueryEntity bolDetailQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(bolDetailQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TransportMgtServiceClientProxy.GetInstance().GetBolDetailByQueryEntity(bolDetailQuery);
            base.BindData2GridView(this.gvBolDetail, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
            bolDetailQuery.CurrentPage = "0";
            this.BindData2BolDetailList(bolDetailQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
            bolDetailQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2BolDetailList(bolDetailQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
            bolDetailQuery.BolID = this.PreviousPage.BolDetailID;
            bolDetailQuery.DeliveryID = this.PreviousPage.DeliveryOrderID;
            bolDetailQuery.OwnerID = this.PreviousPage.OwnerID;
            bolDetailQuery.SkuID = this.PreviousPage.SkuID;
            bolDetailQuery.Carrier = this.PreviousPage.CarrierID;
            bolDetailQuery.Driver = this.PreviousPage.DriverID;
            bolDetailQuery.ActualEquipment = this.PreviousPage.Actualequipment;
            bolDetailQuery.Status = "= 'New'";
            this.BindData2BolDetailList(bolDetailQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
            if (int.Parse(bolDetailQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                bolDetailQuery.CurrentPage = (int.Parse(bolDetailQuery.CurrentPage) + 1).ToString();
                this.BindData2BolDetailList(bolDetailQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
            if (int.Parse(bolDetailQuery.CurrentPage) > 0)
            {
                bolDetailQuery.CurrentPage = (int.Parse(bolDetailQuery.CurrentPage) - 1).ToString();
                this.BindData2BolDetailList(bolDetailQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("BolDetailQuery.aspx");
        }

        private BolDetailQueryEntity GetBolDetailQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as BolDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as BolDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                BolDetailQueryEntity bolDetailQuery = this.GetBolDetailQueryEntity();
                bolDetailQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2BolDetailList(bolDetailQuery);
            }
        }

        protected void gvBolDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvBolDetail_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvBolDetail.DataKeys[e.NewSelectedIndex]["BOL_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        private void LoadData()
        {
            BolDetailQueryEntity bolDetailQuery = new BolDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    bolDetailQuery.BolID = this.PreviousPage.BolDetailID;
                    bolDetailQuery.DeliveryID = this.PreviousPage.DeliveryOrderID;
                    bolDetailQuery.OwnerID = this.PreviousPage.OwnerID;
                    bolDetailQuery.SkuID = this.PreviousPage.SkuID;
                    bolDetailQuery.Carrier = this.PreviousPage.CarrierID;
                    bolDetailQuery.Driver = this.PreviousPage.DriverID;
                    bolDetailQuery.ActualEquipment = this.PreviousPage.Actualequipment;
                    bolDetailQuery.Status = "= 'New'";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            bolDetailQuery.IsGetAll = false;
            bolDetailQuery.CurrentPage = "0";
            bolDetailQuery.PageSize = this.gvBolDetail.PageSize.ToString();
            bolDetailQuery.SortField = "ROWID";
            bolDetailQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvBolDetail.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvBolDetail.Columns[1].Visible = false;
                this.gvBolDetail.Columns[2].Visible = true;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetBolDetailQueryEntity(bolDetailQuery);
            this.BindData2BolDetailList(bolDetailQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                this.LoadData();
            }
        }

        private void SetBolDetailQueryEntity(BolDetailQueryEntity bolDetailQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = bolDetailQuery;
            }
            else
            {
                base.PageQueryEntitySession = bolDetailQuery;
            }
        }

        public BolDetailQuery PreviousPage
        {
            get
            {
                return (BolDetailQuery) base.PreviousPage;
            }
        }
    }
}

